<?php
/**
 * @file
 * Contains the theme's functions to manipulate Drupal's default markup.
 *
 * Complete documentation for this file is available online.
 * @see https://drupal.org/node/1728096
 */


/**
 * Override or insert variables into the maintenance page template.
 *
 * @param array $variables
 *   Variables to pass to the theme template.
 * @param string $hook
 *   The name of the template being rendered ("maintenance_page" in this case.)
 */
/* -- Delete this line if you want to use this function
function nobel_preprocess_maintenance_page(&$variables, $hook) {
  // When a variable is manipulated or added in preprocess_html or
  // preprocess_page, that same work is probably needed for the maintenance page
  // as well, so we can just re-use those functions to do that work here.
  nobel_preprocess_html($variables, $hook);
  nobel_preprocess_page($variables, $hook);
}
// */

/**
 * Override or insert variables into the html templates.
 *
 * @param array $variables
 *   Variables to pass to the theme template.
 * @param string $hook
 *   The name of the template being rendered ("html" in this case.)
 */

function nobel_preprocess_html(&$variables, $hook) {
  $node = menu_get_object();
  if ($node->type == 'minisite') {
    $variables['theme_hook_suggestions'][] = 'html__type__'. $node->type;
  }
}

/**
 * Override or insert variables into the page templates.
 *
 * @param array $variables
 *   Variables to pass to the theme template.
 * @param string $hook
 *   The name of the template being rendered ("page" in this case.)
 */

function nobel_preprocess_page(&$variables, $hook) {

  if (user_is_logged_in() && arg(0) == 'node' && (int) arg(1)) {
    global $user;
    $date = strtotime(date('Y-m-d'));

    $result = db_select('visit_log', 'vl')
      ->fields('vl', array('id'))
      ->condition('uid', $user->uid, '=')
      ->condition('nid', arg(1), '=')
      ->condition('date', $date, '=')
      ->execute()
      ->fetchField();
    if (!$result) {
      db_insert('visit_log')->fields(['nid' => arg(1), 'uid' => $user->uid, 'date' => $date, 'datetime' => time()])->execute();
    }

  }
  if (arg(0) == 'search') {
    $variables['theme_hook_suggestions'][] = 'page__search';
  }
  elseif ($variables['node']->type == 'about_us') {
    $variables['theme_hook_suggestions'][] = 'page__node__' . $variables['node']->type;
  }
  elseif ($variables['node']->type == 'product') {
    $variables['theme_hook_suggestions'][] = 'page__node__' . $variables['node']->type;
  }
  elseif ($variables['node']->type == 'patient') {
    $variables['theme_hook_suggestions'][] = 'page__node__' . $variables['node']->type;
  }
  elseif ($variables['node']->type == 'article') {
    $variables['theme_hook_suggestions'][] = 'page__node__' . $variables['node']->type;
  }
  elseif ($variables['node']->type == 'education_course') {
    $variables['theme_hook_suggestions'][] = 'page__node__' . $variables['node']->type;
  }
  elseif ($variables['node']->type == 'for_oral_rehabilitation') {
    $variables['theme_hook_suggestions'][] = 'page__node__' . $variables['node']->type;
  }
  elseif (strpos(current_path(), 'taxonomy/term/') !== FALSE) {
    $tid = ltrim(current_path(), 'taxonomy/term/');
    $res = taxonomy_get_parents_all($tid);
    if ($res[0]->vid == 5) {
      $variables['theme_hook_suggestions'][] = 'page__taxonomy__term__library';
    }
    elseif ($res[0]->vid == 7) {
      $variables['theme_hook_suggestions'][] = 'page__taxonomy__term__article';
    }
  }
}


/**
 * Override or insert variables into the region templates.
 *
 * @param array $variables
 *   Variables to pass to the theme template.
 * @param string $hook
 *   The name of the template being rendered ("region" in this case.)
 */
/* -- Delete this line if you want to use this function
function nobel_preprocess_region(&$variables, $hook) {
  // Don't use Zen's region--no-wrapper.tpl.php template for sidebars.
  if (strpos($variables['region'], 'sidebar_') === 0) {
    $variables['theme_hook_suggestions'] = array_diff(
      $variables['theme_hook_suggestions'], array('region__no_wrapper')
    );
  }
}
// */

/**
 * Override or insert variables into the block templates.
 *
 * @param array $variables
 *   Variables to pass to the theme template.
 * @param string $hook
 *   The name of the template being rendered ("block" in this case.)
 */
/* -- Delete this line if you want to use this function
function nobel_preprocess_block(&$variables, $hook) {
  // Add a count to all the blocks in the region.
  // $variables['classes_array'][] = 'count-' . $variables['block_id'];

  // By default, Zen will use the block--no-wrapper.tpl.php for the main
  // content. This optional bit of code undoes that:
  if ($variables['block_html_id'] == 'block-system-main') {
    $variables['theme_hook_suggestions'] = array_diff(
      $variables['theme_hook_suggestions'], array('block__no_wrapper')
    );
  }
}
// */

/**
 * Override or insert variables into the node templates.
 *
 * @param array $variables
 *   Variables to pass to the theme template.
 * @param string $hook
 *   The name of the template being rendered ("node" in this case.)
 */
/* -- Delete this line if you want to use this function
function nobel_preprocess_node(&$variables, $hook) {
  $variables['sample_variable'] = t('Lorem ipsum.');

  // Optionally, run node-type-specific preprocess functions, like
  // nobel_preprocess_node_page() or nobel_preprocess_node_story().
  $function = __FUNCTION__ . '_' . $variables['node']->type;
  if (function_exists($function)) {
    $function($variables, $hook);
  }
}
// */

/**
 * Override or insert variables into the comment templates.
 *
 * @param array $variables
 *   Variables to pass to the theme template.
 * @param string $hook
 *   The name of the template being rendered ("comment" in this case.)
 */
/* -- Delete this line if you want to use this function
function nobel_preprocess_comment(&$variables, $hook) {
  $variables['sample_variable'] = t('Lorem ipsum.');
}
// */
//过滤自带的css
function nobel_css_alter(&$css) {
  if (request_path() != 'imce') {
    foreach ($css as $c) {
      if (strpos($c['data'], 'modules/') !== FALSE) {
        unset($css[$c['data']]);
      }
    }
  }
}

//过滤自带的javascript文件
function nobel_js_alter(&$js) {
  if (request_path() != 'imce') {
    foreach ($js as $j) {
      if (is_string($j['data']) && (strpos($j['data'], 'misc/') !== FALSE || strpos($j['data'], 'languages/') !== FALSE)) {
        unset($js[$j['data']]);
        unset($js['settings']);
      }
    }
  }
}

function nobel_preprocess_user_login(&$variables) {
  $variables['form'] = drupal_build_form('user_login', user_login(array(), $form_state)); ## I have to build the user login myself.
}


function nobel_preprocess_user_register_form(&$variables) {
  //$variables['form'] = drupal_build_form('user_register_form', user_register_form(array(), $form_state));

  $variables['form'] = drupal_get_form('user_register_form');
}


function nobel_preprocess_user_pass(&$variables) {
  //$variables['form'] = drupal_build_form('user_register_form', user_register_form(array(), $form_state));
  $variables['form'] = drupal_get_form('user_pass');
}

